package com.zmn.plat.admin.controller.product.tariff.item.city.price;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.product.tariff.item.city.price.ProductTariffItemCityPrice;
import com.zmn.plat.model.vo.product.tariff.item.city.price.ProductTariffItemCityPriceBatchGroupVo;
import com.zmn.plat.model.vo.product.tariff.item.city.price.ProductTariffItemCityPriceBatchVo;
import com.zmn.plat.model.vo.product.tariff.item.city.price.ProductTariffItemCityPriceVo;

/**
 * 城市价格
 *
 * @author linfeng
 * @since 2020-03-03 10:10:14
 */
@RestController
@RequestMapping("/product/tariff/item/city/price/")
public class ProductTariffItemCityPriceModifyController extends ProductTariffItemCityPriceBaseController {

    /**
     * 新增修改
     *
     * @param productFaultItemCityPrice
     * @author linfeng
     * @since 2020-03-03 10:10:14
     */
    @RequestMapping(value = "addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ProductTariffItemCityPrice productFaultItemCityPrice) {
        Date now = DateUtil.getNow();

        Integer relateId = productFaultItemCityPrice.getRelateId();
        try {
            if (relateId == null || relateId == 0) {
                productFaultItemCityPrice.setCreater(getMcStaffName(request));
                productFaultItemCityPrice.setCreateTime(now);
                productFaultItemCityPriceService.insert(productFaultItemCityPrice);
            } else {
                productFaultItemCityPrice.setUpdater(getMcStaffName(request));
                productFaultItemCityPrice.setUpdateTime(now);
                productFaultItemCityPriceService.updateByKey(productFaultItemCityPrice);
            }

            return ResponseDTO.success(true,"操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("不能重复添加！", productFaultItemCityPrice.getItemId());
            return ResponseDTO.fail("数据已经存在，不能重复添加！");
        } catch (Exception ex) {
            logger.error(String.format("[zmn]新增/修改城市价格失败,faultItemCityPrice:[%s],ex:[%s]",
                JSONObject.toJSONString(productFaultItemCityPrice), ex.getMessage()), ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 修改城市价格
     *
     * @return
     */
    @RequestMapping(value = "modifyCityPrice")
    public ResponseDTO modifyCityPrice(@RequestBody ProductTariffItemCityPriceVo productFaultItemCityPriceVo) {
        try {
            LoginStaff mcStaff = getMcStaff();
            if (productFaultItemCityPriceVo.getRelateId() == null) {
                return ResponseDTO.fail("主键ID不能为空！");
            }

            ProductTariffItemCityPrice cityPrice = productFaultItemCityPriceService.findByKey(productFaultItemCityPriceVo.getRelateId());
            cityPrice.setPrice(productFaultItemCityPriceVo.getPrice());
            cityPrice.setNegotiableMinPrice(productFaultItemCityPriceVo.getNegotiableMinPrice());
            cityPrice.setNegotiableMaxPrice(productFaultItemCityPriceVo.getNegotiableMaxPrice());
            cityPrice.setInternalSettlementPrice(productFaultItemCityPriceVo.getInternalSettlementPrice());
            cityPrice.setExternalSettlementPrice(productFaultItemCityPriceVo.getExternalSettlementPrice());
            cityPrice.setUpdater(mcStaff.getRealName());
            cityPrice.setUpdateTime(DateUtil.getNow());
            productFaultItemCityPriceService.updateByKey(cityPrice);
            return ResponseDTO.success(productFaultItemCityPriceVo.getProductId(), "操作成功！");
        } catch (Exception e) {
            logger.error("修改城市价格操作失败,e:{},msg:{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 快速设置城市价格
     *
     * @return
     */
    @RequestMapping(value = "addCityPriceByCityGroup")
    @RequiresPermissions("/product/tariff/item/city/price/cityGroup.action")
    public ResponseDTO addCityPriceByCityGroup(@RequestBody ProductTariffItemCityPriceBatchGroupVo groupVo) {
        if (groupVo.getProductId() == null || groupVo.getTariffId() == null || groupVo.getItemId() == null
            || CollectionUtils.isEmpty(groupVo.getGroupList()) || groupVo.getGroupId() == null) {
            return ResponseDTO.fail("产品ID，价格表ID，项目ID，所选城市分组均不能为空！");
        }
        try {
            LoginStaff mcStaff = getMcStaff();
            productFaultItemCityPriceBService.batchAddCityPriceByCityGroup(mcStaff.getRealName(), groupVo);
            return ResponseDTO.success(true,"添加成功！");
        } catch (PlatException e) {
            logger.error("新增城市价格操作失败,e:{},msg:{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        } catch (Exception e) {
            logger.error("新增城市价格操作失败,e:{},msg:{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 批量新增城市价格
     *
     * @return
     */
    @RequestMapping(value = "batchAddCityPrice")
    public ResponseDTO batchAddCityPrice(@RequestBody ProductTariffItemCityPriceBatchVo productFaultItemCityPriceBatchVo) {
        try {
            LoginStaff mcStaff = getMcStaff();
            if (productFaultItemCityPriceBatchVo.getProductId() == null || productFaultItemCityPriceBatchVo.getTariffId() == null
                || productFaultItemCityPriceBatchVo.getItemId() == null || productFaultItemCityPriceBatchVo.getProvinceCityVOS() == null) {
                return ResponseDTO.fail("产品ID，价格表ID，项目ID，所选城市均不能为空！");
            }

            Integer result = productFaultItemCityPriceBService.batchAddCityPrice(mcStaff.getRealName(), productFaultItemCityPriceBatchVo);
            return ResponseDTO.success(result,"操作成功！");
        } catch (PlatException e) {
            logger.error("新增城市价格操作失败,e:{},msg:{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        } catch (Exception e) {
            logger.error("新增城市价格操作失败,e:{},msg:{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }
    }

    @RequestMapping(value = "delete")
    public ResponseDTO updateStatus(@RequestParam(value = "id", defaultValue = "0") Integer id) {

        if (id == null || id == 0) {
            return ResponseDTO.fail("id不能为空！");
        }

        Integer result = productFaultItemCityPriceService.deleteByKey(id);
        return ResponseDTO.success(result, "操作成功！");
    }

}
